package com.example.idachuappone.order.item;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.cook.item.CookDetailActivity;
import com.example.idachuappone.cook.item.PayPromptlyActivity;
import com.example.idachuappone.order.action.OrderAction;
import com.example.idachuappone.person.entity.Order;
import com.example.idachuappone.person.entity.OrderPriceItem;
import com.example.idachuappone.person.entity.OrderState;
import com.example.idachuappone.utils.AppShareData;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.example.idachuappone.utils.Utils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class MyOrderDetailTwoActivity extends BaseActivity implements OnClickListener {
	private Button btn_all_time;
	private Button btn_back;
	private Button btn_chulizhong_clear;
	private Button btn_comment;
	private Button btn_daichuli_clear;
	private Button btn_fuwuzhong_ok;
	private Button btn_pay_clear;
	private Button btn_pay_ok;
	private BitmapUtils bu;
	private Dialog dialogLoading;
	private ImageView img_head1;
	private LinearLayout ll_cook_no;
	private LinearLayout ll_cook_yes;
	private LinearLayout ll_fuwuzhong;
	private LinearLayout ll_meno;
	private LinearLayout ll_order_youhui_list;
	private LinearLayout ll_pay;
	private Order order;
	private List<OrderState> orderStateliList;
	private String orderid;
	private TextView tv_daimai;
	private TextView tv_meno;
	private TextView tv_name;
	private TextView tv_order_state;
	private TextView tv_order_state_detail;
	private TextView tv_package1;
	private TextView tv_pay_quan_money;
	private TextView tv_pay_shiji_money;
	private TextView tv_service_for;
	private TextView tv_tags_greed;
	private TextView tv_time1;
	private TextView tv_user_address;
	private TextView tv_user_name;
	private TextView tv_user_phone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_order_detail_two);
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		this.dialogLoading = ComUtil.createCusProgressSmallBlackBgDialog(this);
		this.orderid = getIntent().getStringExtra("id");
		initView();
		initData();
	}

	private void initView() {
		this.tv_daimai = (TextView) findViewById(R.id.tv_daimai);
		this.tv_pay_shiji_money = (TextView) findViewById(R.id.tv_pay_shiji_money);
		this.tv_pay_quan_money = (TextView) findViewById(R.id.tv_pay_quan_money);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.btn_all_time = (Button) findViewById(R.id.btn_all_time);
		this.btn_all_time.setOnClickListener(this);
		this.tv_user_name = (TextView) findViewById(R.id.tv_user_name);
		this.tv_user_phone = (TextView) findViewById(R.id.tv_user_phone);
		this.tv_user_address = (TextView) findViewById(R.id.tv_user_address);
		this.ll_cook_yes = (LinearLayout) findViewById(R.id.ll_cook_yes);
		this.tv_service_for = (TextView) findViewById(R.id.tv_service_for);
		this.tv_tags_greed = (TextView) findViewById(R.id.tv_tags_greed);
		this.ll_cook_no = (LinearLayout) findViewById(R.id.ll_cook_no);
		this.img_head1 = (ImageView) findViewById(R.id.img_head1);
		this.tv_package1 = (TextView) findViewById(R.id.tv_package1);
		this.tv_time1 = (TextView) findViewById(R.id.tv_time1);
		this.tv_name = (TextView) findViewById(R.id.tv_name);
		this.ll_pay = (LinearLayout) findViewById(R.id.ll_pay);
		this.btn_pay_clear = (Button) findViewById(R.id.btn_pay_clear);
		this.btn_pay_clear.setOnClickListener(this);
		this.btn_pay_ok = (Button) findViewById(R.id.btn_pay_ok);
		this.btn_pay_ok.setOnClickListener(this);
		this.btn_daichuli_clear = (Button) findViewById(R.id.btn_daichuli_clear);
		this.btn_daichuli_clear.setOnClickListener(this);
		this.btn_chulizhong_clear = (Button) findViewById(R.id.btn_chulizhong_clear);
		this.btn_chulizhong_clear.setOnClickListener(this);
		this.ll_fuwuzhong = (LinearLayout) findViewById(R.id.ll_fuwuzhong);
		this.btn_fuwuzhong_ok = (Button) findViewById(R.id.btn_fuwuzhong_ok);
		this.btn_fuwuzhong_ok.setOnClickListener(this);
		this.btn_comment = (Button) findViewById(R.id.btn_comment);
		this.btn_comment.setOnClickListener(this);
		this.ll_order_youhui_list = (LinearLayout) findViewById(R.id.ll_order_youhui_list);
		this.ll_meno = (LinearLayout) findViewById(R.id.ll_meno);
		this.tv_meno = (TextView) findViewById(R.id.tv_meno);
		this.tv_order_state = (TextView) findViewById(R.id.tv_order_state);
		this.tv_order_state_detail = (TextView) findViewById(R.id.tv_order_state_detail);
	}

	private void hideButton() {
		this.ll_pay.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		this.btn_daichuli_clear.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		this.btn_chulizhong_clear.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		this.ll_fuwuzhong.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		this.btn_comment.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
	}

	private void initData() {
		if (CheckUtil.checkNet(this)) {
			this.dialogLoading.show();
			getOrderTime();
			return;
		}
		MainToast.show(this, getResources().getString(R.string.txt_network), 0);
	}

	private void getOrderTime() {
		NetUtil.get(this, new StringBuilder(Constant.PROMPTLY_TIME).append(this.orderid).toString(), 100,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						try {
							JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
							if (jsonObject.getInt("code") == 10000) {
								int i;
								JSONArray jsonArray = jsonObject.getJSONArray("data");
								List<OrderState> orderStateliList1 = new ArrayList();
								for (i = 0; i < jsonArray.length(); i++) {
									OrderState orderState = new OrderState();
									orderState.parseJson(jsonArray.getJSONObject(i));
									orderStateliList1.add(orderState);
								}
								MyOrderDetailTwoActivity.this.orderStateliList = new ArrayList();
								for (i = orderStateliList1.size() - 1; i >= 0; i--) {
									MyOrderDetailTwoActivity.this.orderStateliList.add(orderStateliList1.get(i));
								}
								MyOrderDetailTwoActivity.this.getOrderDetail();
								return;
							}
							MainToast.show(MyOrderDetailTwoActivity.this, jsonObject.getString("error"), 0);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						NetUtil.netFail(MyOrderDetailTwoActivity.this, MyOrderDetailTwoActivity.this.dialogLoading,
								true);
					}
				});
	}

	private void getOrderDetail() {
		NetUtil.get(this, new StringBuilder(Constant.PROMPTLY_DETAIL).append(this.orderid).toString(), 100,
				new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						try {
							JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
							if (jsonObject.getInt("code") == 10000) {
								MyOrderDetailTwoActivity.this.order = new Order();
								MyOrderDetailTwoActivity.this.order = MyOrderDetailTwoActivity.this.order
										.parseJson(jsonObject.getJSONObject("data"));
								MyOrderDetailTwoActivity.this.setOrderTimeState();
								MyOrderDetailTwoActivity.this.setView();
								return;
							}
							MainToast.show(MyOrderDetailTwoActivity.this, jsonObject.optString("error"), 0);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						NetUtil.netFail(MyOrderDetailTwoActivity.this, MyOrderDetailTwoActivity.this.dialogLoading,
								true);
					}
				});
	}

	private void setOrderTimeState() {
		if (this.orderStateliList != null && this.orderStateliList.size() > 0) {
			String state = new OrderAction().getOrderTimeState(this.orderStateliList.get(0), this.order);
			this.tv_order_state.setText(state.substring(0, state.indexOf(" ")));
			this.tv_order_state_detail.setText(state.substring(state.indexOf(" ") + 1));
		}
	}

	private void setView() {
		int length = BitmapCacheManagementTask.MESSAGE_FLUSH;
		this.dialogLoading.dismiss();
		hideButton();
		if (Integer.parseInt(this.order.getStatus()) == 1) {
			this.btn_daichuli_clear.setVisibility(0);
		} else if (Integer.parseInt(this.order.getStatus()) == 2) {
			this.btn_chulizhong_clear.setVisibility(0);
		} else if (Integer.parseInt(this.order.getStatus()) == 3) {
			if (!(this.order.getKitchener() == null || this.order.getKitchener().getId() == null
					|| this.order.getKitchener().getId().equals("null") || Integer.parseInt(this.order
					.getIs_commented()) != 0)) {
				this.btn_comment.setVisibility(0);
			}
		} else if (Integer.parseInt(this.order.getStatus()) != 4) {
			if (Integer.parseInt(this.order.getStatus()) == 5) {
				this.ll_pay.setVisibility(0);
			} else if (Integer.parseInt(this.order.getStatus()) == 6) {
				this.ll_fuwuzhong.setVisibility(0);
			}
		}
		setServiceAddressView();
		setOrderYouHuiList();
		if (this.order.getKitchener() == null || this.order.getKitchener().getId() == null
				|| this.order.getKitchener().getId().equals("null")) {
			this.ll_cook_yes.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.ll_cook_no.setVisibility(0);
		} else {
			this.ll_cook_yes.setVisibility(0);
			this.ll_cook_no.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.bu.display(this.img_head1, this.order.getKitchener().getHead_small(), new BitmapLoadCallBack<View>() {
				@Override
				public void onLoadCompleted(View view, String arg1, Bitmap bitmap, BitmapDisplayConfig arg3,
						BitmapLoadFrom arg4) {
					((ImageView) view).setImageBitmap(BitmapUtilHelper.getCroppedRoundBitmap(bitmap,
							Utils.dip2px(MyOrderDetailTwoActivity.this, 22.0f)));
				}

				@Override
				public void onLoadFailed(View arg0, String arg1, Drawable arg2) {
				}
			});
			this.tv_name.setText(this.order.getKitchener().getName());
			this.tv_service_for.setText(this.order.getKitchener().getService_for());
			if (this.order.getKitchener().getTags() == null || this.order.getKitchener().getTags().size() <= 0) {
				this.tv_tags_greed.setText("\u64c5\u957f\u5bb6\u5e38\u83dc");
			} else {
				String tag = "";
				if (this.order.getKitchener().getTags().size() < 2) {
					length = this.order.getKitchener().getTags().size();
				}
				for (int i = 0; i < length; i++) {
					tag = new StringBuilder(String.valueOf(tag))
							.append(this.order.getKitchener().getTags().get(i).getName()).append("   ").toString();
				}
				this.tv_tags_greed.setText("\u64c5\u957f" + tag);
			}
			this.ll_cook_yes.setOnClickListener(this);
			this.img_head1.setOnClickListener(this);
		}
		this.tv_pay_shiji_money.setText(ComUtil.FormatDistance(this.order.getPay_price()));
		this.tv_pay_quan_money.setText(new StringBuilder(String.valueOf(ComUtil.FormatDistance(this.order
				.getPackage_price()))).append("\u5143").toString());
		this.tv_package1.setText(new StringBuilder(String.valueOf(this.order.getPackage_name())).append("/")
				.append(this.order.getPackage_price()).append("\u5143").toString());
		this.tv_time1.setText(ComUtil.FormatCalenderTime1(this.order.getDate()));
		if (this.order.getMemo() == null || this.order.getMemo().replace(" ", "").trim().length() <= 0) {
			this.ll_meno.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		} else {
			this.ll_meno.setVisibility(0);
			this.tv_meno.setText(this.order.getMemo());
		}
		if (this.order.getIngredient() == 0) {
			this.tv_daimai.setText("\u4ee3\u4e70\u98df\u6750");
		} else {
			this.tv_daimai.setText("\u81ea\u5907\u98df\u6750");
		}
	}

	private void setOrderYouHuiList() {
		if (this.order.getPrice_item() != null && this.order.getPrice_item().size() > 0) {
			for (int i = 0; i < this.order.getPrice_item().size(); i++) {
				View view = LinearLayout.inflate(this, R.layout.simple_order_detail_youhui_item, null);
				TextView price = (TextView) view.findViewById(R.id.price);
				OrderPriceItem orderPriceItem = this.order.getPrice_item().get(i);
				((TextView) view.findViewById(R.id.name)).setText(orderPriceItem.getItem_name() + ":");
				price.setText("-"
						+ ComUtil.FormatDistance(new StringBuilder(String.valueOf(orderPriceItem.getPrice()))
								.toString()) + "\u5143");
				this.ll_order_youhui_list.addView(view);
			}
		}
	}

	private void setServiceAddressView() {
		this.tv_user_name.setText(this.order.getName());
		this.tv_user_phone.setText(this.order.getPhone());
		this.tv_user_address.setText(new StringBuilder(String.valueOf(this.order.getArea()))
				.append(this.order.getCommunity()).append(this.order.getDoor_number()).toString());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.btn_all_time /* 2131165349 */:
			Intent intentall = new Intent(this, MyOrderDetailTimeTwoActivity.class);
			Bundle bundleall = new Bundle();
			bundleall.putSerializable("orderStateliList", (Serializable) this.orderStateliList);
			bundleall.putSerializable("order", this.order);
			intentall.putExtras(bundleall);
			startActivity(intentall);
		case R.id.img_head1 /* 2131165355 */:
		case R.id.ll_cook_yes /* 2131165356 */:
			if (this.order.getKitchener().getCity_id() == AppShareData.city_id) {
				Intent intentCook = new Intent(this, CookDetailActivity.class);
				intentCook.putExtra("id", this.order.getKitchener().getId());
				startActivity(intentCook);
				return;
			}
			MainToast
					.show(this,
							"\u672c\u53a8\u5e08\u4e0e\u60a8\u4e0d\u518d\u540c\u4e00\u4e2a\u57ce\u5e02\uff0c\u8bf7\u60a8\u9884\u7ea6\u5176\u4ed6\u53a8\u5e08\u5427\uff01",
							0);
		case R.id.btn_pay_clear /* 2131165364 */:
		case R.id.btn_daichuli_clear /* 2131165366 */:
		case R.id.btn_chulizhong_clear /* 2131165367 */:
			new Builder(this).setMessage("\u4eb2\uff0c\u60a8\u786e\u5b9a\u8981\u53d6\u6d88\u8ba2\u5355\u5417\uff1f")
					.setPositiveButton("\u786e\u5b9a", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							MyOrderDetailTwoActivity.this.checkNetClear(MyOrderDetailTwoActivity.this.orderid);
						}
					}).setNegativeButton("\u53d6\u6d88", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
						}
					}).create().show();
		case R.id.btn_pay_ok /* 2131165365 */:
			if (this.order != null) {
				Intent intent = new Intent(this, PayPromptlyActivity.class);
				intent.putExtra("isorder", true);
				intent.putExtra("isprom", false);
				intent.putExtra("orderid", this.order.getId());
				startActivity(intent);
			}
		case R.id.btn_fuwuzhong_ok /* 2131165369 */:
			checkNetFinish(this.orderid);
		case R.id.btn_comment /* 2131165370 */:
			Intent intent1 = new Intent(this, SubmitCommentActivity.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("cookResult", this.order.getKitchener());
			bundle.putString("id", this.order.getId());
			intent1.putExtras(bundle);
			startActivity(intent1);
		default:
		}
	}

	private void checkNetFinish(String id) {
		if (CheckUtil.checkNet(this)) {
			Map<String, String> map = new HashMap();
			map.put("id", id);
			NetUtil.post(this, Constant.PROMPTLY_OK, map, new RequestCallBack<String>() {
				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					try {
						JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
						if (jsonObject.getInt("code") == 10000) {
							AppShareData.isRefreshOrderAll = true;
							AppShareData.isRefreshOrderPay = true;
							AppShareData.isRefreshOrderComment = true;
							MainToast.show(MyOrderDetailTwoActivity.this, "\u6b22\u8fce\u4e0b\u6b21\u4f7f\u7528", 1);
							MyOrderDetailTwoActivity.this.initData();
							return;
						}
						MainToast.show(MyOrderDetailTwoActivity.this, jsonObject.optString("error"), 0);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					MainToast.show(MyOrderDetailTwoActivity.this, MyOrderDetailTwoActivity.this.getResources()
							.getString(R.string.netFail), 0);
				}
			});
			return;
		}
		MainToast.show(this, getResources().getString(R.string.txt_network), 0);
	}

	private void checkNetClear(String id) {
		if (CheckUtil.checkNet(this)) {
			Map<String, String> map = new HashMap();
			map.put("id", id);
			NetUtil.post(this, Constant.PROMPTLY_CLEAR, map, new RequestCallBack<String>() {
				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					try {
						JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
						if (jsonObject.getInt("code") == 10000) {
							AppShareData.isPersonFrag = true;
							AppShareData.isRefreshOrderAll = true;
							AppShareData.isRefreshOrderPay = true;
							AppShareData.isRefreshOrderComment = true;
							MainToast.show(MyOrderDetailTwoActivity.this, "\u8ba2\u5355\u5df2\u53d6\u6d88", 1);
							MyOrderDetailTwoActivity.this.initData();
							return;
						}
						MainToast.show(MyOrderDetailTwoActivity.this, jsonObject.optString("error"), 0);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

				@Override
				public void onFailure(HttpException arg0, String arg1) {
					MainToast.show(MyOrderDetailTwoActivity.this, MyOrderDetailTwoActivity.this.getResources()
							.getString(R.string.netFail), 0);
				}
			});
			return;
		}
		MainToast.show(this, getResources().getString(R.string.txt_network), 0);
	}
}
